<?php defined('SYSPATH') OR die('No direct access allowed.');

class Textimage_Core {
	
	public $fonts;
	public $fontindex;
	public $color;
	public $bgcolor;
	public $text;
	public $size;
	public $font;
	public $savepath;
	public $multiline;
	
	public function __construct(){
	
		$this->fonts[0]="media/fonts/EgyptFRom.ttf";
		$this->fonts[1]="media/fonts/AkkurMon.ttf";
		$this->fonts[2]="media/fonts/TradeGot.ttf";
		
		$this->color="333333";
		$this->bgcolor="ffffff";
		$this->text="hello";
		$this->stext="---";
		$this->font=$this->fonts[0];
		$this->size=20;		
	}
	
	public function setVars($text,$size,$color,$bgcolor,$fontindex){
		$this->text=$text;
		$this->size=$size;
		$this->color=$color;
		$this->bgcolor=$bgcolor;	
		$this->fontindex=$fontindex;
		$this->font=$this->fonts[$fontindex];	
		
		$this->resetSavepath();
	}
	
	public function setText($text){
		$this->text=$text;
		$this->resetSavepath();
		return $this;
	}
	
	public function setSize($size){
		$this->size=$size;
		$this->resetSavepath();
		return $this;
	}
	
	public function setColor($color){
		$this->color=$color;
		$this->resetSavepath();
		return $this;
	}
	
	public function setFontIndex($fontindex){
		$this->fontindex=$fontindex;
		$this->font=$this->fonts[$fontindex];	
		$this->resetSavepath();
	}
	
	private function resetSavepath(){
		$stext=urlencode($this->text);
		$stext=str_replace(array("%",".","/","(",")"," "),"",$stext);
		$stext=substr($stext,0,10);
		$this->savepath="media/resized/txtimg_".$stext."_".$this->size."_".$this->color."_".$this->fontindex."_".$this->multiline.".gif";
	}
	
	public function getImg() {	
		if(file_exists($this->savepath)==true){
			return($this->savepath);
		}else{
			
			
			
			$box=imagettfbbox($this->size,0,$this->font, $this->text);	
			$breite=($box[2]-$box[0])+2;
			$hoehe=round(4*($this->size/3));
			$im = imagecreatetruecolor($breite,$hoehe);
			imagesavealpha($im, true);
			imagealphablending($im, false);
			$bgc=imagecolorallocatealpha($im, 255,255,255,127);
			imagefilledrectangle($im, 0,0,$breite,$hoehe,$bgc);
			$fgc=$this->fromhex($this->color,$im);
			imageantialias($im,true);
			imagefilledrectangle($im, 0, 0,$breite,$hoehe, $bgc);	
			imagettftext($im,$this->size,0,0,$this->size,$fgc,$this->font,$this->text);
			imagepng($im,$this->savepath);
			
			chmod($this->savepath,0777);
			
			return($this->savepath);
			imagedestroy($im);
			
		}
	}
	
	public function getImgMultiline($maxw,$lh){
		
		$this->multiline=$maxw+(10*$lh);
		$this->resetSavepath();
	
		if(file_exists($this->savepath)==true){
			return($this->savepath);
		}else{
			$absarray=explode("<br />", $this->text);
			$wordsarray=array();
			$linesarray=array();
			$lindex=0;
			//vorbereiten
			for($h=0;$h<count($absarray);$h++){
			
				$wordsarray=explode(" ",$absarray[$h]);
				for($i=0;$i< count($wordsarray);$i++){			
					$box=imagettfbbox($this->size,0,$this->font,$linesarray[$lindex]." ".$wordsarray[$i]);
					$breite=($box[2]-$box[0])+2;
					if($breite> $maxw){
						$lindex+=1;
					}
					$linesarray[$lindex].=$wordsarray[$i]." ";	
				}
				$lindex+=1;
			}	
			$hoehe=$lh*count($linesarray);
			//zeichnen
			$im = imagecreatetruecolor($maxw,$hoehe);
			$bgc=$this->fromhex($this->bgcolor,$im);
			$fgc=$this->fromhex($this->color,$im);
			imagecolortransparent($im,$bgc);
			imagefill($im,0,0,$bgc);
			imageantialias($im,true);
			for($i=0;$i<count($linesarray);$i++){
				imagettftext($im,$this->size,0,0,$this->size+($i*$lh),$fgc,$this->font,$linesarray[$i]);	
			}
			imagegif($im,$this->savepath);
			chmod($this->savepath,0777);
			return($this->savepath);
			imagedestroy($im);
		}
	}
	
	private function fromhex($string,$image) {
		sscanf($string, "%2x%2x%2x", $red, $green, $blue);
		return ImageColorAllocate($image,$red,$green,$blue);
	}

}

?>